//
//  PayView.m
//  RJETGeography
//
//  Created by RJET04 on 2023/8/25.
//

#import "PayView.h"

@implementation PayView

- (void)setEnv:(NSString *)env
{
    _env = env;
    
    if ([env isEqualToString:@"zh"]) {
        self.titleLab.text = @"赞赏我们";
        self.detailLab.text = @"给我们赞赏";
        self.wxLab.text = @"微信赞赏";
        self.zfbLab.text = @"支付宝赞赏";
    }else{
        self.titleLab.text = @"Appreciate us";
        self.detailLab.text = @"Give us appreciation";
        self.wxLab.text = @"weixin pay";
        self.zfbLab.text = @"alipay";
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupViews:frame];
    }
    return self;
}

- (void)setupViews:(CGRect)frame
{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    
    CGFloat selfW = frame.size.width;
//    CGFloat selfH = frame.size.height;
    
    __weak typeof(self) weakSelf = self;
    
    self.titleLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(30, 0, selfW-2*30, 50))
        .textBlock(NSLocalizedString(@"payTitle", @""))
        .textAlignmentBlock(NSTextAlignmentCenter)
        .backGroundColorBlock([UIColor whiteColor]);
    }];
    [self addSubview:self.titleLab];
    
    self.closeImageView = [ZYImageView createZYImageView:^(ZYImageView *imageView) {
        imageView.frameBlock(CGRectMake(selfW-35, 15, 20, 20))
        .imageBlock([UIImage imageNamed:@"close"]);
    }];
    [self addSubview:self.closeImageView];
    
    ZYView *closeMaskView = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(selfW-50, 0, 50, 50))
        .backGroundColorBlock([UIColor clearColor])
        .userEnableBlock(YES);
    }];
    [self addSubview:closeMaskView];
    
    //MARK: -- 关闭
    closeMaskView.singalTapBlock = ^{
        
        [weakSelf removeFromSuperview];
        [weakSelf.weakVc dismiss];
    };
    
    ZYView *middleBgV = [ZYView createZYView:^(ZYView *view) {
        
        view.backGroundColorBlock([UIColor colorWithHexString:@"#F8F8F8"])
        .frameBlock(CGRectMake(0, 50, selfW, 250));
    }];
    [self addSubview:middleBgV];
    
    self.iconImageView = [ZYImageView createZYImageView:^(ZYImageView *imageView) {
        imageView.frameBlock(CGRectMake(selfW/2-150/2, 20, 150, 50))
        
        .imageBlock([UIImage imageNamed:@"RJET"]);
        
        imageView.layer.cornerRadius = 25;
        imageView.layer.masksToBounds = YES;
    }];
    [middleBgV addSubview:self.iconImageView];
    
    self.detailLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(0, CGRectGetMaxY(self.iconImageView.frame)+10, selfW, 16))
        .textBlock(NSLocalizedString(@"payDetail", @""))
        .textAlignmentBlock(NSTextAlignmentCenter)
        .fontBlock([UIFont systemFontOfSize:16]);
    }];
    [middleBgV addSubview:self.detailLab];
    
    self.amountTf = [ZYTextField createZYTextField:^(ZYTextField *textField) {
        textField.frameBlock(CGRectMake(20, CGRectGetMaxY(self.detailLab.frame)+20, selfW-2*20, 50))
        .backGroundColorBlock([UIColor whiteColor])
        .textAlignmentBlock(NSTextAlignmentRight)
        .textBlock(@"1");
        
        textField.layer.cornerRadius = 5;
        textField.layer.masksToBounds = YES;
    }];
    [middleBgV addSubview:self.amountTf];
    
    ZYLabel *yuanL = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(0, 0, 60, 50))
        .textBlock(NSLocalizedString(@" RMB  ", @""))
        .textAlignmentBlock(NSTextAlignmentCenter);
    }];
    self.amountTf.rightView = yuanL;
    self.amountTf.rightViewMode = UITextFieldViewModeAlways;
    
    CGFloat lx = 40;
    CGFloat lxpadding = 15;
    CGFloat itemW = (selfW-2*lx-5*lxpadding)/4;
    
//    __weak typeof(middleBgV) weakMiddleBgV = middleBgV;

    for (NSInteger i=0; i<4; i++) {
        
        ZYLabel *lll = [ZYLabel createZYLabel:^(ZYLabel *label) {
            label.frameBlock(CGRectMake(lx+lxpadding+(lxpadding+itemW)*i, CGRectGetMaxY(self.amountTf.frame)+20, itemW, 30))
            .backGroundColorBlock([UIColor whiteColor])
            .textAlignmentBlock(NSTextAlignmentCenter)
            .userEnableBlock(YES)
            .textColorBlock([UIColor blackColor])
            .textBlock(@[@"10", @"20", @"50", @"100"][i])
            .fontBlock([UIFont systemFontOfSize:15]);
            
            label.layer.borderWidth = 1;
            //F37B1D CCCCCC
            label.layer.borderColor = [UIColor colorWithHexString:@"#CCCCCC"].CGColor;
            label.layer.cornerRadius = 2;
            label.layer.masksToBounds = YES;
        }];
        [middleBgV addSubview:lll];
        lll.tag = 1001+i;
        
        if (i == 0) {
            self.lab1 = lll;
        }else if (i == 1){
            self.lab2 = lll;
        }else if (i == 2){
            self.lab3 = lll;
        }else{
            self.lab4 = lll;
        }
        
        __weak typeof(lll) weakL = lll;
        lll.singalTapBlock = ^{
            
            ZYLabel *ll = (ZYLabel *)[middleBgV viewWithTag:weakL.tag];
            weakSelf.amountTf.text = ll.text;
            for (ZYLabel *llab in middleBgV.subviews) {
                if (llab.tag >= 1001 && llab.tag <= 1004) {
                    
                    if(llab.tag == weakL.tag){
                        
                        llab.layer.borderColor = [UIColor colorWithHexString:@"#F37B1D"].CGColor;
                        llab.layer.borderWidth = 1;
                        llab.textColor = [UIColor colorWithHexString:@"#F37B1D"];
                    }else{
                        llab.layer.borderColor = [UIColor colorWithHexString:@"#CCCCCC"].CGColor;
                        llab.layer.borderWidth = 1;
                        llab.textColor = [UIColor blackColor];
                    }
                }
            }
        };
    }
    
    self.line = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(20, CGRectGetMaxY(self.lab1.frame)+20, selfW-2*20, 1))
        .backGroundColorBlock([UIColor colorWithHexString:@"#CCCCCC"]);
    }];
    [middleBgV addSubview:self.line];
    
    ZYView *bottomV = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(0, CGRectGetMaxY(middleBgV.frame), selfW, 60))
        .backGroundColorBlock([UIColor whiteColor]);
    }];
    [self addSubview:bottomV];
    
    self.wxLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(0, 0, selfW/2, 60))
        .textColorBlock([UIColor colorWithHexString:@"#39B54A"])
        .fontBlock([UIFont systemFontOfSize:16])
        .textBlock(NSLocalizedString(@"weixin", @""))
        .textAlignmentBlock(NSTextAlignmentCenter)
        .userEnableBlock(YES)
        .backGroundColorBlock([UIColor clearColor]);
    }];
    [bottomV addSubview:self.wxLab];
    
    ZYView *line2 = [ZYView createZYView:^(ZYView *view) {
        view.frameBlock(CGRectMake(selfW/2-0.5, 10, 1, 40))
        .backGroundColorBlock([UIColor colorWithHexString:@"#CCCCCC"]);
    }];
    [bottomV addSubview:line2];
    
    self.zfbLab = [ZYLabel createZYLabel:^(ZYLabel *label) {
        label.frameBlock(CGRectMake(selfW/2, 0, selfW/2, 60))
        .textColorBlock([UIColor colorWithHexString:@"#39B54A"])
        .fontBlock([UIFont systemFontOfSize:16])
        .textBlock(NSLocalizedString(@"alipay", @""))
        .textAlignmentBlock(NSTextAlignmentCenter)
        .userEnableBlock(YES)
        .backGroundColorBlock([UIColor clearColor]);
    }];
    [bottomV addSubview:self.zfbLab];
    
    //MARKK: -- 微信
    self.wxLab.singalTapBlock = ^{
        
        if (weakSelf.payblock) {
            weakSelf.payblock(1, [weakSelf.amountTf.text floatValue]);
        }
    };
    
    //MARK: - 支付宝
    self.zfbLab.singalTapBlock = ^{
        
        if (weakSelf.payblock) {
            weakSelf.payblock(2, [weakSelf.amountTf.text floatValue]);
        }
    };
    
    
}

@end
